<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="医生名称" prop="name">
            <el-col :span="11">
              <el-select
                v-model="queryParams.userId"
                placeholder="请选择医生名称"
                clearable
                style="width: 240px"
              >
                <el-option v-for="(item,index) in doctorList" :key="item.id" :label="item.doctorName" :value="item.id"/>

              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="请假日期起" prop="status">
            <el-col :span="11">
              <el-date-picker :rows="6" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                              v-model="queryParams.startTime" style="width: 200px;"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="请假日期止" prop="status">
            <el-col :span="11">
              <el-date-picker :rows="6" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                              v-model="queryParams.endTime" style="width: 200px;"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="审核状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="审核状态"
              clearable
              style="width: 240px"
            >
              <el-option :key="0" label="未审核" :value="0"/>
              <el-option :key="1" label="已审核" :value="1"/>
              <el-option :key="2" label="驳回" :value="2"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-col :span="20">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button>
            </el-col>
          </el-form-item>
        </el-form>


        <el-table :data="leaveList" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column prop="doctorName" width="100" label="医生名称" align="center"/>
          <el-table-column v-if="false" prop="id" label="id" align="center" hidden/>
          <el-table-column prop="startTime" label="请假开始时间" align="center"/>
          <el-table-column prop="endTime" label="请假结束时间" align="center"/>
          <el-table-column prop="reason" label="请假原因" align="center">
          </el-table-column>
          <el-table-column prop="status" label="是否审批" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == '未审批'" >未审核</el-tag>
              <el-tag v-if="scope.row.status == '已审批'" type="success">已审批</el-tag>
              <el-tag v-if="scope.row.status == '已驳回'" type="danger">驳回</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="rejectReason" label="驳回原因" align="center"/>

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template v-if="scope.row.status =='未审批'" slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handle(scope.row)"
              >同意
              </el-button>
              <el-button
                size="mini"
                type="text"
                style="color: red"
                icon="el-icon-delete"
                @click="handlereject(scope.row)"
              >拒绝
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">

            <el-form-item label="驳回原因" prop="reason">
              <el-input
                v-model="form.reason"
                placeholder="请输入驳回原因"
                maxlength="30"
              />
            </el-form-item>

          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {allLeaveList, updateLeaveStatus} from "@/api/leave/leave";
import {allDoctorList} from "@/api/info/doctor";

export default {
  name: 'User',
  data() {
    let self = this;
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        startTime:undefined,
        endTime: undefined,
        status: undefined
      },
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        reason: [
          {required: true, trigger: 'blur', message: '驳回原因不能为空'}
        ]
      },

      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 用户表格数据
      leaveList: null,
      doctorList: null,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {},
      reason:undefined,
      queryDoctorParams: {
        pageNum: 1,
        pageSize: 100,
        userName: undefined,
        departmentId: undefined
      },
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    getList() {
      this.loading = true
      allLeaveList(this.queryParams).then((response) => {
        this.leaveList = response.list
        this.total = response.total
        this.loading = false
      })
      allDoctorList(this.queryDoctorParams).then((response) => {
        this.doctorList = response.list;
      });
    },

    // 表单重置
    reset() {
      this.reason =undefined
    },

    handle(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否同意' + row.doctorName + '的请假审批')
        .then(function () {
          return updateLeaveStatus(ids, '', '1')
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('审批成功')
        })
        .catch(() => {
        })
    },
    handlereject(row){
      this.ids = row.id
      this.open = true
      this.reason =undefined
      this.title = '驳回原因'
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          updateLeaveStatus(this.ids, this.form.reason, '2').then((response) => {
            this.$modal.msgSuccess('驳回成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
